﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Xml . Linq;

namespace WenceyWang . RichMan4L . Maps
{
	public class OneWayRoad : Road
	{
		long entranceID;

		Road entrance = null;

		public virtual Road Entrance { get { return entrance ?? ( entrance = Map . Currnet . GetRoad ( entranceID ) ); } set { entranceID = value . Id; entrance = value; } }


		long exitID;

		Road exit = null;

		public virtual Road Exit { get { return exit ?? ( exit = Map . Currnet . GetRoad ( exitID ) ); } set { exitID = value . Id; exit = value; } }


		public override bool CanEnter ( Road road ) { return road == Entrance; }

		public override Path Route ( Road previous , int moveCount , Path result = null )
		{
			if ( previous == null )
			{
				throw new ArgumentNullException ( nameof ( previous ) );
			}
			if ( !CanEnter ( previous ) )
			{
				throw new ArgumentException ( "无法通过previous进入此道路" , nameof ( previous ) );
			}
			if ( moveCount < 0 )
			{
				throw new ArgumentOutOfRangeException ( nameof ( moveCount ) );
			}
			Path current = result ?? new Path ( );
			current . Route . Add ( this );
			if ( BlockMoving || moveCount == 0 )
			{
				return current;
			}
			else
			{
				return Exit . Route ( this , moveCount - 1 , result );
			}
		}

		public OneWayRoad ( XElement resource ) : base ( resource )
		{
			entranceID = Convert . ToInt64 ( resource . Attribute ( nameof ( Entrance ) ) . Value );
			exitID = Convert . ToInt64 ( resource . Attribute ( nameof ( Exit ) ) . Value );
		}

	}
}
